﻿using System.Collections.Generic;
using DynamicChart.Service.FunctionConversion;
using Newtonsoft.Json.Linq;

namespace DynamicChart.Service.Step.GenerateSQL;

public abstract class GenerateAbstractSQLStep : IGenerateSQLStep
{
    protected readonly ISqlFunctionAdapter _adapter;

    public GenerateAbstractSQLStep(ISqlFunctionAdapter adapter)
    {
        _adapter = adapter;
    }
    
    protected string CreateField(Dictionary<string, object> entity, string columnKey, string fieldName)
    {
        return CreateField(entity, columnKey, fieldName, null);
    }
    
    protected string CreateField(Dictionary<string, object> entity, string columnKey, string fieldName, string format)
    {
        if (!entity.TryGetValue(fieldName, out var value))
            return string.Empty;

        string name = value.ToString()!;
        
        var strategy = GetStrategy(name);
        return strategy.Convert(format).Replace(":fieldName", columnKey);
    }

    protected IConvertStrategy GetStrategy(string input)
    {
        return ConvertStrategyFactory.GetStrategy(input, _adapter);
    }
    
    public abstract void Process(IDictionary<string, object> data, IDictionary<string, string> res);
}